package academic.concordia.bankofquebec.managedbean;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import org.springframework.context.annotation.Scope;

import academic.concordia.bankofquebec.bean.mutualfunds.IMutualFundsBean;
import academic.concordia.bankofquebec.bean.session.SessionData;
import academic.concordia.bankofquebec.entity.AccountAttributes;
import academic.concordia.bankofquebec.entity.CheckingsAccount;
import academic.concordia.bankofquebec.entity.Client;
import academic.concordia.bankofquebec.entity.MutualFunds;

@ManagedBean(name="mutualFundsOpeningManagedBean")
@Scope("session")
public class MutualFundsOpeningManagedBean implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private MutualFunds mutualFunds;
	
	@ManagedProperty(value="#{SessionData}")
	private SessionData sessionData;

	@ManagedProperty(value="#{MutualFundsBean}")
	private IMutualFundsBean mutualFundsBean;
	
	@PostConstruct
	void initialize(){
		this.mutualFunds = new MutualFunds();
	}
	
	public String openAccount(){
		Client client = sessionData.getClient();
		System.out.println("Client: " + client.toString());
		if (!mutualFundsBean.createMutualFund(client, mutualFunds)){
			FacesContext facesContext = FacesContext.getCurrentInstance();
			FacesMessage facesMessage = new FacesMessage("Client already have this product");
			facesContext.addMessage(null, facesMessage);
			return "";
		}
		
		CheckingsAccount checkingsAccount = (CheckingsAccount) client.getProduct(AccountAttributes.CHECKINGS_ACCOUNT.name());
		mutualFundsBean.transferInitialInvestment(mutualFunds, checkingsAccount);
		
		return "products";
	}
	
	public SessionData getSessionData() {
		return sessionData;
	}

	public void setSessionData(SessionData sessionData) {
		this.sessionData = sessionData;
	}

	public MutualFunds getMutualFunds() {
		return mutualFunds;
	}

	public void setMutualFunds(MutualFunds mutualFunds) {
		this.mutualFunds = mutualFunds;
	}

	public IMutualFundsBean getMutualFundsBean() {
		return mutualFundsBean;
	}

	public void setMutualFundsBean(IMutualFundsBean mutualFundsBean) {
		this.mutualFundsBean = mutualFundsBean;
	}
	
	

}
